import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    userInfo: {},
    navArr:['作品','分享','答疑'],
    contentList: [1,2],
    showComment: false,
    inputContent: '',
    nowClickedId: '',
    nowClickedComment: []
  },
  onLoad: function() {
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1     //这里的数字设置当前页面在tabbar里对应的序列，从0开始
      })
    }

    //获取用户的本地缓存数据，userinfo信息是在用户授权登录时保存的
    var info = wx.getStorageSync("userinfo");
    //当本地缓存的用户名称不为""或者null时，设置userinfo信息
    if(info.nickName != '' && info.nickName != null){
       this.setData({
           userInfo: info,
       })
     }
     this.flushContent();
  },
  flushContent() {
    const self = this;
    let idx = -1;
    if (this.data.navArr[1] == '作品') {
      idx = 1;
    }
    if (this.data.navArr[1] == '分享') {
      idx = 0;
    }
    if (this.data.navArr[1] == '答疑') {
      idx = 2;
    }
    wx.request({
      // url: 'http://47.120.31.85:8080/word/getStory',
      url: 'http://47.120.31.85:8080/word/flush?type=' + idx + '&username=' + self.data.userInfo.nickName,
      method: 'GET',
      success: function(res) {
        console.log(res)
        self.setData({
          contentList: res.data.data
        })
      }
    })
  },
  changeNav(data) {
    let index = data.currentTarget.dataset.item
    if ( index == 1 ) return;
    const self = this;
    let first = self.data.navArr[0]
    let second = self.data.navArr[1]
    let third = self.data.navArr[2]
    if ( index == 0 ) {
      self.setData({
        navArr:[third,first,second]
      })
    }
    if ( index == 2 ) {
      self.setData({
        navArr:[second,third,first]
      })
    }
    this.flushContent();
  },
  like(data) {
    const self = this;
    let id = data.currentTarget.dataset.item
    wx.request({
      // url: 'http://47.120.31.85:8080/word/getStory',
      url: 'http://47.120.31.85:8080/word/like?id=' + id + '&username=' + self.data.userInfo.nickName,
      method: 'Post',
      success: function(res) {
        console.log('点赞成功')
        self.flushData(id,self.flushContentList)
      }
    })
    
  },

  // 回调函数
  flushContentList(newObj,unChangeIdx) {
    let newValue = []
    if ( unChangeIdx == 0 ) {
      newValue.push(this.data.contentList[0])
      newValue.push(newObj)
    }else {
      newValue.push(newObj)
      newValue.push(this.data.contentList[1])
    }
    this.setData({
      contentList: newValue
    })
  },

  // 点赞 评论 分享后刷新数据
  flushData(id,callback) {
    const self = this;
    let idx = 0;
    console.log(id)
    for ( let i = 0 ; i < self.data.contentList.length ; i ++ ) {
      if (self.data.contentList[i].id == id) {
        idx = i
      }
    }
    let unChangeIdx = 1 - idx;
    let nowObj = self.data.contentList[idx]
    wx.request({
      url: 'http://47.120.31.85:8080/word/getCommunityById?id=' + nowObj.id + '&username=' + self.data.userInfo.nickName,
      method: 'Get',
      success: function(res) {
        nowObj.isLike = res.data.data.isLike;
        nowObj.likeCount = res.data.data.likeCount;
        nowObj.shareCount = res.data.data.shareCount;
        nowObj.commentCount = res.data.data.commentCount;
        callback(nowObj,unChangeIdx)
      }
    })
  },

  publishComment() {
    const self = this;
    const formData = {
      content: self.data.inputContent,
      communityId: self.data.nowClickedId
    }
    wx.request({
      url: 'http://47.120.31.85:8080/word/publishComment?username=' + self.data.userInfo.nickName,
      method: 'Post',
      data: formData,
      success: function(res) {
        self.setData({
          inputContent: ''
        })
        Toast.success('评论成功!');
      }
    })
  },

  openComment(data) {
    const self = this;
    let id = data.currentTarget.dataset.item
    this.setData({
      showComment: true,
      nowClickedId: id
    })
    wx.request({
      url: 'http://47.120.31.85:8080/word/getCommentById?commentId=' + id,
      method: 'GET',
      success: function(res) {
        self.setData({
          nowClickedComment: res.data.data
        })
      }
    })
  },

  onClickHide() {
    this.setData({ showComment: false });
  },

  toPublishPage() {
    wx.navigateTo({
      url: '../publish/publish'
    })
  },

  inputContentChange(e) {
    this.setData({
      inputContent: e.detail.value//e.detail.value表示输入框的当前值
    })
  },
})